#include "editstudent.h"
#include "ui_editstudent.h"
#include <QDebug>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>

#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>

#include <QMessageBox>



EditStudent::EditStudent(int sno,QWidget *parent) :
    QWidget(parent),
    ui(new Ui::EditStudent)
{
    ui->setupUi(this);
    this->sno=sno;
    qDebug()<<sno;

    QNetworkAccessManager *manger = new QNetworkAccessManager(this);
    // 使用get方式发送http请求并将sno一起发送给服务器，得到学员具体信息
    QString url = QString("http://127.0.0.1/school/api/v1.0/student/%1").arg(sno);
    manger->get(QNetworkRequest(QUrl(url)));

    // 请求成功后将数据显示到表单中
    connect(manger, &QNetworkAccessManager::finished, [this](QNetworkReply *reply){
        // 获取 Web服务器 返回的字符串，并转为 json对象
        QString result = reply->readAll();
        qDebug()<<result;
        qDebug()<<result.toUtf8();
        QJsonDocument jsonDoc = QJsonDocument::fromJson(result.toUtf8());
        QJsonObject obj = jsonDoc.object();
        //把data数据（jsonvalue格式）转为json对象
        QJsonObject studentObj = obj.value("data").toObject();
        // 将json对象显示到表单中
        ui->nameLineEdit->setText(studentObj.value("sname").toString());
        ui->nickLineEdit->setText(studentObj.value("snickname").toString());
        ui->comboBox->setCurrentText(studentObj.value("sgender").toString());
        ui->ageLineEdit->setText(QString::number(studentObj.value("sage").toInt()));
        ui->deptLineEdit->setText(QString::number(studentObj.value("sdept").toInt()));
    });



}

EditStudent::~EditStudent()
{
    delete ui;
}

//点击编辑表单中的修改按钮，能够将数据提交给Web服务器进行修改
void EditStudent::on_pushButton_clicked()
{
    //收集表单数据
    QString name = ui->nameLineEdit->text();
    QString nickname = ui->nickLineEdit->text();
    QString gender = ui->comboBox->currentText();
    QString age = ui->ageLineEdit->text();
    QString dept = ui->deptLineEdit->text();
    qDebug()<<name<<nickname<<gender<<age<<dept;

    QNetworkAccessManager *manager=new QNetworkAccessManager(this);
    //发送put请求  将数据一起发送给服务器
    // 设置请求头  固定格式 和post相同
    QNetworkRequest request;
    request.setHeader(QNetworkRequest::ContentTypeHeader,"application/x-www-form-urlencoded");
    // 设置请求的 Url地址
    request.setUrl(QUrl(QString("http://127.0.0.1/school/api/v1.0/student/%1").arg(this->sno)));
    //配置发送到服务端的数据
    QString params = QString("sname=%1&snickname=%2&sgender=%3&sage=%4&sdept=%5").arg(name)
            .arg(nickname).arg(gender).arg(age).arg(dept);
    // 发送 post请求  请求对象  发送到的数据
    manager->put(request,params.toUtf8());


    //处理响应
    connect(manager,&QNetworkAccessManager::finished,[this](QNetworkReply *reply){
        //把响应转换为json对象 获取其中信息
        QString str=reply->readAll();
        qDebug()<<str;
        QJsonDocument jsonDoc = QJsonDocument::fromJson(str.toUtf8());
        QJsonObject obj = jsonDoc.object();

        if (obj.value("code").toInt() == 200)
        {
            QMessageBox::information(this, "成功", "修改信息成功");
            this->close();
            // 当数据添加完成后，调用自定义信号，在主界面中来更新表格数据
            emit this->updateSucess();
        }
        else
        {
            QMessageBox::critical(this, "失败", "修改信息失败");
        }
    });
}
